package algorithms.leecode;

import java.util.HashSet;
import java.util.Set;

public class Au128 {
    public static void main(String[] args) {
        int[] nums = {0,3,7,2,5,8,4,6,1};
        System.err.println(new Au128().longestConsecutive(nums));
    }
    public int longestConsecutive(int[] nums) {
        Set<Integer> num_set = new HashSet<Integer>();
        for (int num : nums) {
            num_set.add(num);
        }
        int longestStreak = 0;
        for (int num : num_set) {
            // 目的相当于排好序
            if (!num_set.contains(num - 1)) {
                int currentNum = num;
                int currentStreak = 1;
                while (num_set.contains(currentNum + 1)) {
                    currentNum += 1;
                    currentStreak += 1; }
                longestStreak = Math.max(longestStreak, currentStreak);
            } }
        return longestStreak; }
}
